//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/.
// 			VisualNovelToolkit		     /_/_/_/_/_/_/_/_/_/.
// Copyright ©2013 - Sol-tribe.	/_/_/_/_/_/_/_/_/_/.
//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/.
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
using ViNoToolkit;

[ CustomEditor( typeof( DialogPartNode ) ) ] 
[ System.Serializable ]
public class DialogPartNodeInspector : Editor {

		#region ForViNoSoundPlayerMember	
	static private ViNoSoundPlayer m_SoundPlInstance;
	static private string[] m_BgmEntries;
	static private string[] m_SeEntries;
	static private string[] m_VoiceEntries;	

	static public string[] bgmEntries{
		get{	
			return m_BgmEntries;
		}
	}

	static public string[] seEntries{
		get{	
			return m_SeEntries;
		}
	}

	static public string[] voiceEntries{
		get{	
			return m_VoiceEntries;
		}
	}
	#endregion
	
		#region PrivateMembers
	// DialogPartNode members.
	static private int m_ViewMode = 0;
	static private string[] m_MenuItems ={ "Actions" , "Edit Text" , "XML" };
		#endregion

	void OnEnable( ){			
		DialogPartNode dlgNode = ( target as DialogPartNode );

		dlgNode.FindTextBoxObjects();

		// Assign DialogID.
		dlgNode.ReAssignDialogIDsInThisObject();
		
		if( m_SoundPlInstance == null ){
			m_SoundPlInstance = GameObject.FindObjectOfType( typeof( ViNoSoundPlayer ) ) as ViNoSoundPlayer;
		}		

		if( m_SoundPlInstance != null ){
			m_BgmEntries = m_SoundPlInstance.GetSoundEntryNames();	
			m_VoiceEntries = m_SoundPlInstance.GetVoiceEntryNames();	
			m_SeEntries = m_SoundPlInstance.GetSeEntryNames();
		}
	}
		
	public override void OnInspectorGUI(){
		OnGUI_DialogPart( target as DialogPartNode );
	}

	/// <summary>
	/// Raises the GUI dialog part event.
	/// </summary>
	/// <param name="targetNode">Target node.</param>
	void OnGUI_DialogPart( DialogPartNode targetNode ){
		ViNoEditorGUILayout.DrawDialogTextBoxAndNameField( targetNode );

		// Actions | Edit Text | XML .
		m_ViewMode = GUILayout.Toolbar( m_ViewMode , m_MenuItems );

		string menu = m_MenuItems[ m_ViewMode ];

		// View Mode : XML.
		if( menu == "XML" ){
			ViNoEditorUtil.BeginGreenColor();
		
				ViNoEditorGUILayout.DrawXMLImportField( targetNode );

			ViNoEditorUtil.EndGUIColor();	
		}
		// Actions or Edit Text Mode.
		else{
			if( menu == "Actions" ){							
				// CheckALL | UNCheckALL | Swap CheckItems.
				EditorGUILayout.BeginHorizontal();
				if( GUILayout.Button( "Check " + System.Environment.NewLine + " ALL" )){
					for( int i=0;i<targetNode.dlgDataList.Count;i++){
						targetNode.dlgDataList[ i ].active = true;
					}
				}

				if( GUILayout.Button( "UNCheck" + System.Environment.NewLine + "ALL" )){
					for( int i=0;i<targetNode.dlgDataList.Count;i++){
						targetNode.dlgDataList[ i ].active = false;
					}
				}
				
				if( GUILayout.Button( "Swap Checked"+ System.Environment.NewLine + "2 Items" )){
					DialogPartNodeUtility.Swap2Items( targetNode );
				}
		
				GUI.enabled = true;

				EditorGUILayout.EndHorizontal();
			}

			if( targetNode.dlgDataList != null ){
				for( int i=0;i<targetNode.dlgDataList.Count;i++){
					DialogPartData unit = targetNode.dlgDataList[ i ];
					if( menu == "Actions"  ){//&& unit.actionID != DialogPartNodeActionType.Dialog ){
						NodeGUI.DrawDialogItemBar( targetNode , ref unit , i , targetNode.m_ViNoTextBox , targetNode.m_NameTextBox  );					
					}
					NodeGUI.OnGUI_a( targetNode , ref unit , i , targetNode.m_ViNoTextBox , targetNode.m_NameTextBox , m_ViewMode );				
				}		
			}
			else{
				DialogPartNodeUtility.Initialize( targetNode );
			}
	
			// Draw + | - .
			ViNoEditorGUILayout.DrawDialogListUtil( targetNode );
		}
	}	

}
